package personal.vivian.interceptor;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import personal.vivian.builder.MethodInfo;
import personal.vivian.common.Primitives;

import java.lang.reflect.Method;

public class MethodInterc implements MethodInterceptor {
    private MethodInfo methodInfo;
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        methodInfo=new MethodInfo(this,method,objects);
        //TODO
        Class<?> c=method.getReturnType();

        if(Primitives.isPrimitive(c)){
            return Primitives.getDefaultVal(c);
        }
        return null;
    }
    public Object getInstance(final Class<?> c){
        final Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(c);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    public MethodInfo getMethodInfo() {
        return methodInfo;
    }

    public void setMethodInfo(MethodInfo methodInfo) {
        this.methodInfo = methodInfo;
    }
}
